package com.trippin.servlets;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.trippin.DB.DBHandler;
import com.trippin.DB.entities.AttractionEntity.ShortDetailedAttraction;
import com.trippin.DB.entities.SharingRequest;
import com.trippin.DB.entities.UserNotification;
import com.trippin.DB.entities.UserSuggestion;
import com.trippin.Utils.ServletUtil;
import com.trippin.Utils.ServletUtilConstants;

@SuppressWarnings("serial")
public class NotificationServlet extends HttpServlet {

	private static final Logger logger = Logger.getLogger(NotificationServlet.class.getCanonicalName());

	private static DBHandler dbHandlerInstance = DBHandler.INSTANCE;

	@Override
	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		Action action = ServletUtil.getActionFromHeader(req,resp,NotificationServlet.class.toString());		
		long userId  = ServletUtil.getLongFromHeader(req, resp, ServletUtilConstants.USER_ID,
				NotificationServlet.class.toString(), action.getName());

		long sharingReqId = -1;	
		long tripId = -1;

		switch(action) {
		case ACCEPT_TRIP_SHARING_REQUEST:
		case DENIED_TRIP_SHARING_REQUEST:
			sharingReqId = ServletUtil.getLongFromHeader(req, resp, ServletUtilConstants.SHARING_REQUEST_ID,
					NotificationServlet.class.toString(), action.getName());
			break;
		case GET_USERS_NEAR_ME:
		case ATTRACTION_YOU_USER_MAY_LIKE:
			tripId = ServletUtil.getLongFromHeader(req, resp, ServletUtilConstants.TRIP_ID,
					TripServlet.class.toString(), action.getName());
			break;
		}

		try {
			String bodyResponse = "";
			switch(action) {

			case GET_TRIP_SHARING_REQUESTS:
				bodyResponse = getTripSharingRequests(userId);
				break;

			case ACCEPT_TRIP_SHARING_REQUEST:
				bodyResponse = acceptTripSharingRequset(userId, sharingReqId);
				break;

			case DENIED_TRIP_SHARING_REQUEST:
				bodyResponse = deniedTripSharingRequset(userId, sharingReqId);

			case GET_USERS_NEAR_ME:
				bodyResponse = getUsersNearMe(userId, tripId);
				break;
			case ATTRACTION_YOU_USER_MAY_LIKE:
				bodyResponse = getAttractionsUserMayLike(userId, tripId);
				break;

			default:
				ServletUtil.printError(resp, action.getName(), NotificationServlet.class.getName(), ServletUtilConstants.ERROR_WRONG_ACTION_NAME);
				return;
			}

			ServletUtil.createResponse(resp, action.getName(), bodyResponse);


		} catch(Exception e) {
			ServletUtil.printError(resp,action.getName(), NotificationServlet.class.getName(), e.getMessage());
			logger.log(Level.SEVERE,e.toString(), e);
		}
	}


	private String deniedTripSharingRequset(long userId, long sharingReqId) throws IOException {
		UserNotification<SharingRequest> sharingRequests = dbHandlerInstance.deniedTripSharingRequset(userId, sharingReqId);
		return ServletUtil.getResponseBody(sharingRequests);
	}


	private String acceptTripSharingRequset(long userId, long sharingReqId) throws IOException {
		UserNotification<SharingRequest> sharingRequests = dbHandlerInstance.acceptTripSharingRequset(userId, sharingReqId);
		return ServletUtil.getResponseBody(sharingRequests);
	}


	private String getAttractionsUserMayLike(long userId, long tripId) {
		UserNotification<ShortDetailedAttraction> attrsUserMayLike = dbHandlerInstance.getAttractionsUserMayLike(userId, tripId);
		return ServletUtil.getResponseBody(attrsUserMayLike);
	}


	private String getUsersNearMe(long userId, long tripId) {
		UserNotification<UserSuggestion> suggestions = dbHandlerInstance.getUsersNearMe(userId, tripId);
		return ServletUtil.getResponseBody(suggestions);
	}


	private String getTripSharingRequests(long userId) throws IOException {
		UserNotification<SharingRequest> sharingRequests = dbHandlerInstance.getUserTripSharingRequests(userId);
		return ServletUtil.getResponseBody(sharingRequests);
	}

}
